const { projectModel } = require('../models/model/project');
const { dataToJSON, dataDeleteOrUpdateRes } = require('../utils/tools');

// 添加
exports.createService = async (data) => {
	let result = await projectModel.create(data);
	return dataToJSON(result, '');
};

// 删除
exports.removeService = async (id) => {
	const result = await projectModel.destroy({ where: { id } });
	return dataDeleteOrUpdateRes(result);
};

// 修改
exports.updateService = async (data, id) => {
	const result = await projectModel.update(data, { where: { id } });
	return dataDeleteOrUpdateRes(result);
};

// 查单个
exports.findByIdService = async (id) => {
	const result = await projectModel.findOne({
		where: { id }
	});
	return dataToJSON(result, '');
};

// 查分页
exports.findByPageService = async (query) => {
	const size = Number(query.size) || 1;
	const limit = Number(query.limit) || 100;
	const params = {
		limit,
		offset: (size - 1) * limit,
		where: {}
	};
	if (query.title) {
		params.where.title = {
			[Op.like]: `%${query.title}%`
		};
	}
	const result = await projectModel.findAll(params);
	return JSON.parse(JSON.stringify(result));
};
